package sun.it.dao.impl;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import sun.it.dao.UserDao;
import sun.it.domain.User;
import sun.it.util.JDBCUtils;

import java.util.List;

public class UserDaoimpl implements UserDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public User CheckLogin(User user) {

        try {
            String sql="select *from user1  where username=? and password =?";
            User user1 = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), user.getUsername(), user.getPassword());
            return user1;
        } catch (DataAccessException e) {
           return null;
        }

    }

    @Override
    public int Regised(User user) {
        String sql="INSERT into user1(username,password) VALUES('"+user.getUsername()+"','"+user.getPassword()+"')";
//        String sql=" INSERT into mauser(username,password) " +
//                "VALUES('"+user.getUsername()+"','"+user.getPassword()+"'";
        try {
            return template.update(sql);
        } catch (DataAccessException e) {
            return 0;
        }

    }

    @Override
    public String LookupPas(String username) {
        String sql= "SELECT password from user1 WHERE username='"+username+"'";
        String password = template.queryForObject(sql, String.class);
        //template.
        return password;
    }

    @Override
    public int UpdateUP(User user) {
        System.out.println(user.getUsername()+" "+user.getPassword());
        String sql="UPDATE user1 set password='"+user.getPassword()+"' WHERE username='"+user.getUsername()+"'";
        try {
            int update = template.update(sql);
            System.out.println(update);
            return update;
        } catch (DataAccessException e) {
            return 0;
        }

    }
}
